package com.zhongwang.business.module.estimateBudgetAccountingManage.excelListener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zhongwang.business.common.ImportCallBack;
import com.zhongwang.business.gen.entity.BusBudgetAccountingCost;
import com.zhongwang.business.gen.entity.BusBudgetAccountingTask;
import com.zhongwang.business.gen.entity.BusGeneralHours;
import com.zhongwang.business.gen.entity.BusMaterialData;
import com.zhongwang.business.module.estimateBudgetAccountingManage.dao.AccountingInfoDao;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.excelImport.AccOtherHoursImportModel;
import com.zhongwang.business.module.estimateBudgetAccountingManage.model.excelImport.AccOutImportModel;
import com.zhongwang.business.util.BeanUtil;
import com.zhongwang.business.util.ShiroUtils;
import com.zhongwang.business.util.StringUtils;

import java.util.*;

/**
 * @author 徐一贺
 * @className AccOtherHoursImportListener
 * @description 其他工时信息导入监听器
 * @date 2020/11/21 14:46
 * @versron ver 1.0
 */
public class AccOtherHoursImportListener extends AnalysisEventListener<AccOtherHoursImportModel> {
  /** 钩子 */
  private ImportCallBack callBack;
  /** Excel相关参数模型 */
  private BusMaterialData param;
  /** 材料信息数据持久层 */
  private AccountingInfoDao accountingInfoDao;
  /** 错误列表 */
  private List<AccOtherHoursImportModel> errorList = new ArrayList<>();
  /** 持久化列表 */
  private List<AccOtherHoursImportModel> persistenceList = new ArrayList<>();
  /** 当前时间 */
  private Date currentTime = new Date();
  /** 专业部分信息 */
  private Map<String, List<String>> majorParts;

  /**
   * 构造函数
   *
   * @param param 页面参数
   * @param callBack 回调函数
   * @param accountingInfoDao 持久层
   */
  public AccOtherHoursImportListener(
      BusMaterialData param, ImportCallBack callBack, AccountingInfoDao accountingInfoDao) {
    this.callBack = callBack;
    this.param = param;
    this.accountingInfoDao = accountingInfoDao;
    majorParts = new HashMap<>();
    BusBudgetAccountingCost findPartListParam = new BusBudgetAccountingCost();
    findPartListParam.setBatId(param.getBatId());
    findPartListParam.setMajor("机械");
    List<String> mechanicsPart = accountingInfoDao.findPartListByBatIdAndMajor(findPartListParam);
    findPartListParam.setMajor("流体");
    List<String> fluidPart = accountingInfoDao.findPartListByBatIdAndMajor(findPartListParam);
    findPartListParam.setMajor("自动化");
    List<String> automationPart = accountingInfoDao.findPartListByBatIdAndMajor(findPartListParam);
    majorParts.put("机械", mechanicsPart);
    majorParts.put("流体", fluidPart);
    majorParts.put("自动化", automationPart);
  }

  /**
   * 验证
   *
   * @param data 每行数据
   */
  @Override
  public void invoke(AccOtherHoursImportModel data, AnalysisContext context) {
    String errorInfo = "";
    boolean isRightMajor = false;
    if (data.getMajor() == null || StringUtils.trimAllBlank(data.getMajor()).equals("")) {
      errorInfo = "请填写专业！\r\n";
    } else if (data.getMajor() != null && data.getMajor().length() > 255) {
      errorInfo = "专业填写不正确，正确的是机械、流体或自动化！\r\n";
    }
    if (data.getMajor().equals("机械")
        || data.getMajor().equals("流体")
        || data.getMajor().equals("自动化")) {
      isRightMajor = true;
    } else {
      errorInfo = "专业填写不正确，正确的是机械、流体或自动化！\r\n";
    }
    if (data.getPart() == null || StringUtils.trimAllBlank(data.getPart()).equals("")) {
      errorInfo = "填写部分！\r\n";
    } else if (data.getPart() != null && data.getPart().length() > 255) {
      errorInfo = "部分不可超过255个字符！\r\n";
    } else if (data.getPart() != null && data.getMajor() != null && isRightMajor) {
      List<String> parts = majorParts.get(data.getMajor());
      if (!parts.contains(data.getPart())) {
        errorInfo = "部分填写错误，该专业下无此部分！\r\n";
      }
    }
    if (data.getLaborName() == null || StringUtils.trimAllBlank(data.getLaborName()).equals("")) {
      errorInfo = "请填写工时类别！\r\n";
    }
    if (data.getLaborNum() == null) {
      errorInfo = "请填写数量！\r\n";
    }
    if (data.getUnit() == null || StringUtils.trimAllBlank(data.getUnit()).equals("")) {
      errorInfo = "请填写单位！\r\n";
    } else if (data.getUnit() != null && data.getUnit().length() > 255) {
      errorInfo = "单位不可超过255个字符！\r\n";
    }else if (data.getUnit() != null && !data.getUnit().equals("工日")) {
      errorInfo = "单位只可填写工日！\r\n";
    }

    if (data.getTimeType() == null || StringUtils.trimAllBlank(data.getTimeType()).equals("")) {
      errorInfo = "请填写工时分类！\r\n";
    } else if (data.getTimeType() != null
        && !StringUtils.trimAllBlank(data.getTimeType()).equals("")
        && !data.getTimeType().equals("下料工时")
        && !data.getTimeType().equals("铆焊工时")
        && !data.getTimeType().equals("装配工时")
        && !data.getTimeType().equals("喷涂工时-喷砂处理")
        && !data.getTimeType().equals("喷涂工时-涂装处理")
        && !data.getTimeType().equals("设计工时")
        && !data.getTimeType().equals("调试工时")
        && !data.getTimeType().equals("安装工时")
        && !data.getTimeType().equals("包装工时")) {
      errorInfo = "工时分类填写错误，工时分类只能填写下料工时、铆焊工时、装配工时、喷涂工时-喷砂处理、喷涂工时-涂装处理、设计工时、调试工时、安装工时或包装工时！\r\n";
    }
    if (errorInfo.length() > 0) {
      data.setErrorInfo(errorInfo);
      errorList.add(data);
    } else {
      persistenceList.add(data);
    }
  }

  /**
   * 持久化
   *
   * @param context
   */
  @Override
  public void doAfterAllAnalysed(AnalysisContext context) {
    if (errorList.size() > 0) {
      callBack.onWarning(errorList);
    } else {
      Date currentDate = new Date();
      BusBudgetAccountingTask order =
          accountingInfoDao.getBusBudgetAccountingTaskModelById(param.getBatId());
      for (int i = 0; i < persistenceList.size(); i++) {
        AccOtherHoursImportModel persistenceData = persistenceList.get(i);
        persistenceData.setBatId(param.getBatId());
        persistenceData.setDataUseType(2);
        persistenceData.setGetDate(currentDate);
        persistenceData.setGetUid(ShiroUtils.getUserId());
        persistenceData.setPriceType(1);
        persistenceData.setProCode(order.getProCode());
        BusBudgetAccountingCost setCost = null;
        BusBudgetAccountingCost getBusBudgetAccountingCostCondition = new BusBudgetAccountingCost();
        if(persistenceData.getTimeType().equals("下料工时") && persistenceData.getMajor().equals("机械")){
          persistenceData.setType(7);
          persistenceData.setLaborType(4);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-制造费-下料");
        }else if(persistenceData.getTimeType().equals("下料工时") && persistenceData.getMajor().equals("流体")){
          persistenceData.setType(8);
          persistenceData.setLaborType(4);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-制造费-下料");
        }else if(persistenceData.getTimeType().equals("下料工时") && persistenceData.getMajor().equals("自动化")){
          persistenceData.setType(9);
          persistenceData.setLaborType(4);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-制造费-下料");
        }else if(persistenceData.getTimeType().equals("铆焊工时") && persistenceData.getMajor().equals("机械")){
          persistenceData.setType(13);
          persistenceData.setLaborType(6);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-制造费-铆焊");
        }else if(persistenceData.getTimeType().equals("铆焊工时") && persistenceData.getMajor().equals("流体")){
          persistenceData.setType(14);
          persistenceData.setLaborType(6);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-制造费-铆焊");
        }else if(persistenceData.getTimeType().equals("铆焊工时") && persistenceData.getMajor().equals("自动化")){
          persistenceData.setType(15);
          persistenceData.setLaborType(6);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-制造费-铆焊");
        }else if(persistenceData.getTimeType().equals("装配工时") && persistenceData.getMajor().equals("机械")){
          persistenceData.setType(31);
          persistenceData.setLaborType(13);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-制造费-装配");
        }else if(persistenceData.getTimeType().equals("装配工时") && persistenceData.getMajor().equals("流体")){
          persistenceData.setType(32);
          persistenceData.setLaborType(13);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-制造费-装配");
        }else if(persistenceData.getTimeType().equals("装配工时") && persistenceData.getMajor().equals("自动化")){
          persistenceData.setType(33);
          persistenceData.setLaborType(13);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-制造费-装配");
        }else if(persistenceData.getTimeType().equals("喷涂工时-喷砂处理") && persistenceData.getMajor().equals("机械")){
          persistenceData.setType(25);
          persistenceData.setLaborType(9);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-制造费-喷涂");
        }else if(persistenceData.getTimeType().equals("喷涂工时-喷砂处理") && persistenceData.getMajor().equals("流体")){
          persistenceData.setType(26);
          persistenceData.setLaborType(9);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-制造费-喷涂");
        }else if(persistenceData.getTimeType().equals("喷涂工时-喷砂处理") && persistenceData.getMajor().equals("自动化")){
          persistenceData.setType(27);
          persistenceData.setLaborType(9);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-制造费-喷涂");
        }else if(persistenceData.getTimeType().equals("喷涂工时-涂装处理") && persistenceData.getMajor().equals("机械")){
          persistenceData.setType(25);
          persistenceData.setLaborType(11);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-制造费-喷涂");
        }else if(persistenceData.getTimeType().equals("喷涂工时-涂装处理") && persistenceData.getMajor().equals("流体")){
          persistenceData.setType(26);
          persistenceData.setLaborType(11);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-制造费-喷涂");
        }else if(persistenceData.getTimeType().equals("喷涂工时-涂装处理") && persistenceData.getMajor().equals("自动化")){
          persistenceData.setType(27);
          persistenceData.setLaborType(11);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-制造费-喷涂");
        }else if(persistenceData.getTimeType().equals("设计工时") && persistenceData.getMajor().equals("机械")){
          persistenceData.setType(1);
          persistenceData.setLaborType(1);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-设计费");
        }else if(persistenceData.getTimeType().equals("设计工时") && persistenceData.getMajor().equals("流体")){
          persistenceData.setType(2);
          persistenceData.setLaborType(1);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-设计费");
        }else if(persistenceData.getTimeType().equals("设计工时") && persistenceData.getMajor().equals("自动化")){
          persistenceData.setType(3);
          persistenceData.setLaborType(1);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-设计费");
        }else if(persistenceData.getTimeType().equals("调试工时") && persistenceData.getMajor().equals("机械")){
          persistenceData.setType(1);
          persistenceData.setLaborType(20);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-现场安装费-人工-调试人工");
        }else if(persistenceData.getTimeType().equals("调试工时") && persistenceData.getMajor().equals("流体")){
          persistenceData.setType(2);
          persistenceData.setLaborType(20);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-现场安装费-人工-调试人工");
        }else if(persistenceData.getTimeType().equals("调试工时") && persistenceData.getMajor().equals("自动化")){
          persistenceData.setType(3);
          persistenceData.setLaborType(20);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-现场安装费-人工-调试人工");
        }else if(persistenceData.getTimeType().equals("安装工时") && persistenceData.getMajor().equals("机械")){
          persistenceData.setType(38);
          persistenceData.setLaborType(18);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-现场安装费-人工-安装人工");
        }else if(persistenceData.getTimeType().equals("安装工时") && persistenceData.getMajor().equals("流体")){
          persistenceData.setType(39);
          persistenceData.setLaborType(18);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-现场安装费-人工-安装人工");
        }else if(persistenceData.getTimeType().equals("安装工时") && persistenceData.getMajor().equals("自动化")){
          persistenceData.setType(40);
          persistenceData.setLaborType(18);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(persistenceData.getMajor());
          getBusBudgetAccountingCostCondition.setPart(persistenceData.getPart());
          getBusBudgetAccountingCostCondition.setCode(persistenceData.getMajor() + "-" + persistenceData.getPart() + "-现场安装费-人工-安装人工");
        }else if(persistenceData.getTimeType().equals("包装工时") && persistenceData.getMajor().equals("机械")){
          persistenceData.setType(34);
          persistenceData.setLaborType(15);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(null);
          getBusBudgetAccountingCostCondition.setPart(null);
          getBusBudgetAccountingCostCondition.setCode("包装运输费-包装费");
        }else if(persistenceData.getTimeType().equals("包装工时") && persistenceData.getMajor().equals("流体")){
          persistenceData.setType(34);
          persistenceData.setLaborType(15);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(null);
          getBusBudgetAccountingCostCondition.setPart(null);
          getBusBudgetAccountingCostCondition.setCode("包装运输费-包装费");
        }else if(persistenceData.getTimeType().equals("包装工时") && persistenceData.getMajor().equals("自动化")){
          persistenceData.setType(34);
          persistenceData.setLaborType(15);
          getBusBudgetAccountingCostCondition.setBatId(param.getBatId());
          getBusBudgetAccountingCostCondition.setMajor(null);
          getBusBudgetAccountingCostCondition.setPart(null);
          getBusBudgetAccountingCostCondition.setCode("包装运输费-包装费");
        }
        setCost = accountingInfoDao.getBusBudgetAccountingCostByCondition(getBusBudgetAccountingCostCondition);
        persistenceData.setCostId(setCost.getId());
        BusGeneralHours data = new BusGeneralHours();
        BeanUtil.copy(persistenceData, data);
        accountingInfoDao.insertBusGeneralHours(data);
        setCost.setDataSource("1");
        accountingInfoDao.updateBusBudgetAccountingCostDataSourceByPrimaryKey(setCost);
      }
      callBack.onSuccess();
    }
  }

  /**
   * 异常处理
   *
   * @param exception 异常信息
   * @param context
   */
  @Override
  public void onException(Exception exception, AnalysisContext context) {}
}
